// pages/account/edit/edit.js
var common = require('../../../utils/common.js');
var app = getApp()
Page({
  data: {
    normalInfo: {},
    userInfo: {},
    sexes:[{value:1,text:'男'},{value:2,text:'女'}],
    sex:0,
    type:1,  // 用户类型 1-普通用户，2-教练
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    that.setData({
      type:parseInt(options.type)
    });
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
      common.asynPost(common.API.user.normal, {}, function (data) {
        var type1 = (parseInt(options.type) == 2 && 2) || data['userInfo'].type;
        that.setData({
          normalInfo: data['userInfo'],
          sex:data['userInfo'].sex,
          type:type1
        });
      });
    });
  },
  submitUserInfo:function(e){
    let that = this;
    let normal = that.formData(e.detail.value);
    common.asynPost(common.API.user.edit,normal,function(data){
      if(that.data.type == 2){
        wx.navigateTo({ url: '../coach/coach?normal'+ JSON.stringify(normal) })
      }else{
        wx.showToast({title:'信息保存成功'});
      }
    });
  },
  formData:function(info){
    if(info){
      info.sex = this.data.sex;
      info.type = this.data.type;
    }
    return info;
  },
  sexChange:function(e){
    this.setData({sex : e.detail.value});
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})